<template>
  <div>
    <nav-text><span slot="navText">当前位置: 数据中心 > 设置 > 大区设置</span></nav-text>
    <!-- 搜索框条件 -->
    <div class="searchBox">
      <div>
        <selectBox>
          <span slot="titleName">大区名称:</span>
          <el-select
            slot="selectBox"
            v-model="regonNames"
            clearable
            placeholder="请选择大区名"
          >
            <el-option
              v-for="item in regonNamesOption"
              :key="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </selectBox>
      </div>
      <div>
        <textBox>
          <span slot="titleName">负 责 人 :</span>
          <input
            class="textBoxSize"
            type="text"
            slot="textBox"
            placeholder="请输入负责人"
          />
        </textBox>
      </div>
      <div>
        <selectBox>
          <span slot="titleName">运行状态:</span>
          <el-select
            slot="selectBox"
            v-model="status"
            clearable
            placeholder="请选择运行状态"
          >
            <el-option
              v-for="item in statusOption"
              :key="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </selectBox>
      </div>
      <div class="layoutBtn">
        <div>
          <search-button>
            <el-button
              @click.stop="regionSetList('1',regonNames,principal,status)"
              slot="searchBtn"
              type="success"
              size="mini"
            >搜 索</el-button>
          </search-button>
        </div>
        <div>
          <download-button>
            <el-button
              @click.stop="showaddBox"
              slot="dounloadBtn"
              type="primary"
              icon='el-icon-plus'
              circle
            ></el-button>
          </download-button>
        </div>
      </div>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table
        :header-cell-style="headStyle"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        border
        :data="tableData"
        :fit="fit"
        :highlight-current-row='highlight'
      >
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="dataId"
          label="序 号"
          min-width="40"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="code"
          label="大 区 I D"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="name"
          label="大 区 名 称"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="cName"
          label="负 责 人"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="isUsing"
          label="状 态"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="editTime"
          label="更 新 时 间"
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="principal"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              @click.stop="showAlterBox(scope.row)"
              class="el-button-details"
              size="medium "
              type="text"
            >修 改</el-button>
            <el-button
              @click.stop="showDeleteBox(scope.row)"
              class="el-button-delete"
              size="medium "
              type="text"
            >删 除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页器 -->
    <!-- 分页器 -->
    <pagination>
      <el-pagination
        @current-change="pageNumLink($event,regonNames,principal,status)"
        slot="pagination"
        background
        layout="prev, pager, next, jumper"
        :total="pageTotal"
        :page-size="pageSize"
        hide-on-single-page
      >
      </el-pagination>
    </pagination>
    <!-- 删除弹框 -->
    <delete-box v-if="deleteBoxStates">
      <button
        slot="cancel"
        @click.stop='cancelDeleteBox'
      >取 消</button>
      <button
        slot="affirm"
        @click.stop="affirmDeleteBox"
      >确 认</button>
    </delete-box>
    <!-- 新增数据弹框 isShowAddData -->
    <baseDataBox v-if="isShowAddData">
      <span slot="title">新 增 数 据</span>
      <li slot="content">
        <div>
          <baseInput>
            <label
              for=""
              slot="label"
            >大区ID:</label>
            <input
              v-model="addBoxID"
              slot="input"
              class="inputTextStyle"
              placeholder="请输入大区ID"
              type="text"
            />
          </baseInput>
        </div>
        <div>
          <baseInput>
            <label
              for=""
              slot="label"
            >大区名称:</label>
            <input
              v-model="addBoxAreaName"
              slot="input"
              class="inputTextStyle"
              placeholder="输入大区名称,如:华北大区"
              type="text"
            />
          </baseInput>
        </div>
      </li>
      <li slot="content">
        <div>
          <baseInput>
            <label
              for=""
              slot="label"
            >负责人:</label>
            <input
              @input="getPrincipalList(addBoxPrincipal)"
              v-model="addBoxPrincipal"
              slot="input"
              class="inputTextStyle"
              placeholder="请输入负责人"
              type="text"
            />
          </baseInput>
          <!-- 添加弹框负责人列表组件 -->
          <hospital-quest-box
            v-if="isShowPrincipalList"
            class="boxHospitalList"
          >
            <span
              slot="spanBox"
              v-if="principalNallData"
            >无数据</span>
      <li
        slot="liList"
        @click.stop="selectPrincipal(index)"
        v-for="(item,index) in principalDataList"
        :key="index"
      >{{item}}</li>
      </hospital-quest-box>
  </div>
  <div>
    <whiteSelectBox>
      <span
        class="stateText"
        slot="titleName"
      >状 态 :</span>
      <el-select
        class="selectBoxStyle"
        slot="selectBox"
        v-model="addBoxState"
        clearable
        placeholder="请选择大区"
      >
        <el-option
          v-for="item in addBoxStateOption"
          :key="item"
          :value="item"
        >
        </el-option>
      </el-select>
    </whiteSelectBox>
  </div>
  </li>
  <button
    slot="delete"
    @click.stop="cancelAddBox"
  >取 消</button>
  <button
    slot="affirm"
    @click.stop="affirmAddBox"
  >确 认</button>
  </baseDataBox>
  <!-- 修改数据弹窗 isShowAlterData -->
  <baseDataBox v-if="isShowAlterData">
    <span slot="title">修 改 数 据</span>
    <li slot="content">
      <div>
        <baseInput>
          <label
            for=""
            slot="label"
          >大区名称:</label>
          <input
            v-model="alterBoxName"
            slot="input"
            class="inputTextStyle"
            placeholder="输入大区名,如:华北大区"
            type="text"
          />
        </baseInput>
      </div>

      <div>
      </div>
    </li>
    <li slot="content">
      <div>
        <baseInput>
          <label
            for=""
            slot="label"
          >负责人:</label>
          <input
            @input="getPrincipalList(alterBoxPrincipal)"
            v-model="alterBoxPrincipal"
            slot="input"
            class="inputTextStyle"
            placeholder="请输入负责人"
            type="text"
          />
        </baseInput>
        <!-- 修改弹框中负责人列表组件 -->
        <hospital-quest-box
          v-if="isShowPrincipalList"
          class="boxHospitalList"
        >
          <span
            slot="spanBox"
            v-if="principalNallData"
          >无数据</span>
    <li
      slot="liList"
      @click.stop="alterSelectPrincipal(index)"
      v-for="(item,index) in principalDataList"
      :key="index"
    >{{item}}</li>
    </hospital-quest-box>
    </div>
    <div>
      <whiteSelectBox>
        <span
          class="stateText"
          slot="titleName"
        >状 态 :</span>
        <el-select
          class="selectBoxStyle"
          slot="selectBox"
          v-model="alterBoxState"
          clearable
          placeholder="请选择状态"
        >
          <el-option
            v-for="item in addBoxStateOption"
            :key="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </whiteSelectBox>
    </div>
    </li>
    <button
      slot="delete"
      @click.stop="cancelAlterBox"
    >取 消</button>
    <button
      slot="affirm"
      @click.stop="affirmAlterBox"
    >确 认</button>
  </baseDataBox>
  </div>
</template>

<script>
import hospitalQuestBox from "@/components/DataCenter/BaseForm/hospitalQuestBox";
import whiteSelectBox from "@/components/DataCenter/BaseForm/whiteSelectBox";
import { regionSetList } from "@/request/api.js"; //花括号中的变量是该文件中的函数
import { principalQuest } from "@/request/api.js";
import { areaSetAddData } from "@/request/api.js";
import { areaSetAlterData } from "@/request/api.js";
import { regionSetDelete } from "@/request/api.js";
import navText from "@/components/DataCenter/MyLocation/myLocation";
import textBox from "@/components/DataCenter/BaseForm/textBox";
import searchButton from "@/components/DataCenter/BaseForm/searchBtn";
import downloadButton from "@/components/DataCenter/BaseForm/downloadBtn";
import selectBox from "@/components/DataCenter/BaseForm/selectBox";
import dataSelectBox from "@/components/DataCenter/BaseForm/dateSelectBox";
import pagination from "@/components/baseCpn/pagination.vue";

import deleteBox from "@/components/DataCenter/BaseForm/deleteBox.vue";
import baseDataBox from "@/components/DataCenter/BaseForm/baseDataBox";
import baseInput from "@/components/DataCenter/BaseForm/baseInput";
import "@/assets/base/dataCenterTableStyle.css";
export default {
  components: {
    navText,
    textBox,
    searchButton,
    downloadButton,
    selectBox,
    dataSelectBox,
    pagination,
    deleteBox,
    baseDataBox,
    baseInput,
    whiteSelectBox,
    hospitalQuestBox
  },
  mounted() {
    this.regionSetList(this.$page);
    this.regonNamesOption = localStorage.getItem("allArea").split(",");
  },
  data() {
    return {
      //添加框负责人列表属性
      isShowPrincipalList: false,
      principalNallData: false,
      principalDataList: [],
      principalID: [],
      addBoxprincipalID: "",
      //修改框负责人列表属性
      alterBoxPrincipalValue: "",
      alterBoxprincipalID: null,

      pageTotal: null,
      pageSize: 8,
      isShowAddData: false,
      isShowAlterData: false,
      deleteBoxStates: false,
      highlight: true,
      fit: true,
      Status: "",
      statusOption: ["启用", "禁用"],
      headStyle: {
        height: "7rem",
        backgroundColor: "#131428",
        color: "#FFFFFF",
        border: "1px solid #792a4c",
        fontSize: "1.9rem"
      },
      cellStyle: {
        height: "7rem",
        border: "1px solid #792a4c",
        fontSize: "1.8rem"
      },
      tableData: [],
      regonNames: "",
      principal: "",
      status: "",
      regonNamesOption: [],
      //添加弹框属性
      addBoxID: "",
      addBoxAreaName: "",
      addBoxPrincipal: "",
      addBoxState: "",
      addBoxStateOption: ["启用", "禁用"],
      //修改弹框属性
      alterBoxName: "",
      alterBoxPrincipal: "",
      alterBoxState: "",
      alterDataID: null,
      //删除弹框属性
      deleteBoxID: null
    };
  },
  methods: {
    //添加框选中的负责人
    selectPrincipal(index) {
      this.addBoxPrincipal = this.principalDataList[index];
      this.addBoxprincipalID = this.principalID[index];
      console.log("uid为", this.addBoxprincipalID);
      this.isShowPrincipalList = false;
    },
    //获取弹框负责人列表
    getPrincipalList(addBoxPPValue) {
      console.log("触发了");
      principalQuest({
        name: addBoxPPValue,
        page: "1",
        pageSize: "1000"
      })
        .then(res => {
          this.principalDataList = [];
          this.principalID = [];
          if (res.data.success) {
            this.isShowPrincipalList = true;
            this.principalNallData = false;
            console.log("获取负责人列表成功", res.data.rows);
            for (var i = 0; i < res.data.rows.length; i++) {
              this.principalDataList.push(res.data.rows[i].name); //所有负责人，[]类型
              this.principalID.push(res.data.rows[i].id); //所有负责人ID,数组[]类型,每项为数字类型
              console.log("哈哈哈", this.principalDataList);
              console.log("滴滴滴", this.principalID);
            }
          } else {
            this.principalNallData = true;
            his.isShowPrincipalList = false;
          }
        })
        .catch(err => {
          console.log("获取负责人列表失败", err);
        });
    },
    //修改框选中的负责人
    alterSelectPrincipal(index) {
      this.alterBoxPrincipal = this.principalDataList[index]; //选中的负责人
      this.alterBoxprincipalID = this.principalID[index]; //选中的负责人ID
      console.log("UID是：", this.alterBoxprincipalID);
      this.isShowPrincipalList = false;
    },

    pageNumLink(e, aNames, cNames, isUsings) {
      let E = String(e);
      if (e) {
        this.regionSetList(E, aNames, cNames, isUsings);
      }
    },
    regionSetList(page, aNames, cNames, isUsings) {
      console.log(page, aNames, cNames, isUsings);
      regionSetList({
        page: page,
        pageSize: this.$pageSize,
        aName: aNames,
        cName: cNames,
        isUsing: isUsings
      })
        .then(res => {
          console.log("大区设置列表请求成功", res.data);
          if (res.data.success) {
            this.tableData = res.data.rows;
            // 表格序号内容
            this.pageTotal = res.data.total;
            for (var i = 0; i < res.data.rowcount; i++) {
              var k = (page - 1) * 8 + i + 1;
              this.tableData[i].dataId = k;
            }
          } else {
            this.$message.error({
              message: res.data.errorMsg
            });
          }
        })
        .catch(err => {
          console.log("大区设置列表请求失败", err);
        });
    },
    showaddBox() {
      this.isShowAddData = true;
    },
    cancelAddBox() {
      this.isShowAddData = false;
    },
    //添加框确认按钮-添加数据接口
    affirmAddBox() {
      this.isShowAddData = false;
      areaSetAddData({
        code: this.addBoxID,
        name: this.addBoxAreaName,
        cId: this.addBoxprincipalID, //addBoxprincipalID
        isUsing: this.addBoxState == "启用" ? "1" : "0"
      })
        .then(res => {
          this.regionSetList(this.$page);
          if (res.data.success) {
            this.$message.success({
              message: "新增数据成功"
            });
          } else {
            this.$message.error({
              message: res.data.errorMsg
            });
          }
          console.log("新增数据成功", res);
        })
        .catch(err => {
          console.log("新增数据失败", err);
        });
    },

    showDeleteBox(e) {
      console.log(e);
      this.deleteBoxID = e.id;
      this.deleteBoxStates = true;
    },
    cancelDeleteBox() {
      this.deleteBoxStates = false;
    },
    affirmDeleteBox() {
      this.deleteBoxStates = false;
      //删除数据接口
      regionSetDelete({
        areaId: this.deleteBoxID,
        isUsing: "-1"
      })
        .then(res => {
          console.log("删除数据接口成功", res);
          this.regionSetList(this.$page);
          if (res.data.success) {
            this.$message.success({
              message: "删除数据成功"
            });
          } else {
            this.$message.error({
              message: res.data.errorMsg
            });
          }
        })
        .catch(err => {
          console.log("删除数据接口失败", err);
        });
    },
    showAlterBox(e) {
      console.log(e);
      this.alterBoxName = e.name;
      this.alterBoxPrincipal = e.cName;
      this.alterBoxState = e.isUsing;
      this.isShowAlterData = true;
      this.alterDataID = e.id;
    },
    cancelAlterBox() {
      this.isShowAlterData = false;
    },
    affirmAlterBox() {
      this.isShowAlterData = false;
      //修改数据接口
      areaSetAlterData({
        areaId: this.alterDataID,
        name: this.alterBoxName,
        cId: this.alterBoxprincipalID,
        isUsing: this.alterBoxState == "启用" ? "1" : "0"
      })
        .then(res => {
          this.regionSetList(this.$page);
          if (res.data.success) {
            this.$message.success({
              success: "新增数据成功"
            });
          } else {
            this.$message.error({
              message: res.data.errorMsg
            });
          }
          console.log("修改数据成功", res);
        })
        .catch(err => {
          console.log("修改数据失败", err);
        });
    },
    rowStyle(row) {
      if (row.rowIndex % 2 === 0) {
        return { backgroundColor: "#222644" };
      } else if (row.rowindex % 2 === 1) {
        return { backgroundColor: "#262b4b" };
      }
    }
  }
};
</script>

<style lang='scss' scoped>
.searchBox {
  width: 100%;
  height: 16rem;
  display: flex;
  div {
    flex: 1;
    display: flex;
    flex-direction: column;
  }
  .layoutBtn {
    display: flex;
    flex-direction: row;
    div {
      flex: 1;
      display: flex;
    }
  }
}
.tableBox {
  width: 95%;
  height: 63.5rem;
  margin: 0 auto;
  padding-top: 2rem;
  overflow: hidden;
  .el-button-details {
    font-size: 1.6rem;
    color: #33ffff;
    margin-right: 0.8rem;
  }
  .el-button-delete {
    font-size: 1.6rem;
    color: #ff9cc6;
  }
}
</style>